package org.wutopia.labcloud.domain.uaa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.wutopia.labcloud.domain.uaa.controller.vo.input.UserProductCartAddInputVO;
import org.wutopia.labcloud.domain.uaa.controller.vo.input.UserProductCartDeleteInputVO;
import org.wutopia.labcloud.domain.uaa.controller.vo.output.UserCartGetLIstOutputVO;
import org.wutopia.labcloud.domain.uaa.controller.vo.output.UserProductSpecificationOutput;
import org.wutopia.labcloud.domain.uaa.dao.USerProductCartDao;
import org.wutopia.labcloud.domain.uaa.dao.entity.UserProductCart;
import org.wutopia.labcloud.domain.uaa.service.UserShoppingCartService;
import org.wutopia.labcloud.service.api.service.dubbo.DubboProductService;
import org.wutopia.labcloud.service.api.service.dubbo.FileService;
import org.wutopia.labcloud.service.api.service.dubbo.dto.output.DubboGetProductOutputDTO;
import org.wutopia.labcloud.service.api.service.dubbo.dto.output.DubboProductSpecificationOutput;

import java.util.ArrayList;
import java.util.List;


@Service
@AllArgsConstructor
@Slf4j
public class UserProductCartServiceImpl implements UserShoppingCartService {

    private USerProductCartDao userProductCartDao;

    @DubboReference
    private DubboProductService dubboProductService;

    @DubboReference
    private FileService fileService;

    @Override
    public List<UserCartGetLIstOutputVO> getList(Long userId) {

        QueryWrapper<UserProductCart> queryWrapper  = new QueryWrapper<>();

        queryWrapper.eq("user_id", userId);

        List<UserProductCart> userProductCarts = userProductCartDao.selectList(queryWrapper);

        List<UserCartGetLIstOutputVO> list = new ArrayList<>();

        for (UserProductCart cartItem: userProductCarts) {



            DubboGetProductOutputDTO productById = dubboProductService.getProductById(cartItem.getProductId());

            UserCartGetLIstOutputVO output = new UserCartGetLIstOutputVO();

            output.setId(cartItem.getId());
            output.setUserId(cartItem.getUserId());
            output.setProductId(cartItem.getProductId());

            output.setDescription(productById.getDescription());
            output.setRate(productById.getRate());
            output.setPrice(productById.getPrice());
            output.setTitle(productById.getTitle());

            String[] split = productById.getCoverAddr().split(":");
            String coverUrl = fileService.getFileUrl(split[0], split[1]);

            output.setCoverAddr(coverUrl);
            output.setStockpile(productById.getStockpile());

            String[] split1 = productById.getDetailAddr().split(":");
            String detailUrl = fileService.getFileUrl(split1[0], split1[1]);

            output.setDetailAddr(detailUrl);
            output.setCreateTime(productById.getCreateTime());
            output.setUpdateTime(productById.getUpdateTime());

            List<UserProductSpecificationOutput> specList = new ArrayList<>();

            for (DubboProductSpecificationOutput spec: productById.getSpecificationList()) {
                UserProductSpecificationOutput specOut = new UserProductSpecificationOutput();
                specOut.setId(spec.getId());
                specOut.setItem(spec.getItem());
                specOut.setValue(spec.getValue());

                specList.add(specOut);
            }

            output.setSpecificationList(specList);

            list.add(output);
        }


        return list;
    }

    @Override
    public void add(UserProductCartAddInputVO vo) {

        UserProductCart cart = new UserProductCart();

        cart.setProductId(vo.getProductId());
        cart.setUserId(vo.getUserId());

        userProductCartDao.insert(cart);
    }

    @Override
    public void delete(UserProductCartDeleteInputVO vo) {

        List<Long> cartItemIdList = vo.getCartItemIdList();

        for(Long cartItemId : cartItemIdList) {
            userProductCartDao.deleteById(cartItemId);
        }
    }
}
